<template>
  <div>
  <div><h1>合作商管理</h1></div>
  <div>
    <el-form :model="ruleForm"  ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="电话号码">
        <el-input type="text" v-model="ruleForm.tel" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="短信验证码">
        <el-input type="password" v-model="ruleForm.code" autocomplete="off"></el-input>
        <el-button @click="getCode">获取验证码</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="mylogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>
<script>
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'LoginView',
  data () {
    return {
      ruleForm: {
        tel: '',
        code: '',
        codeKey: ''
      }
    }
  },
  methods: {
    getCode () {
      this.ruleForm.codeKey = uuidv4()
      this.axios.post('/user/getCode', this.ruleForm).then(res => {
        if (res.data.code === 200) {
          this.$message.success('获取成功')
        } else {
          this.$message.success('获取成功')
        }
      })
    },
    mylogin () {
      this.axios.post('/user/mylogin', this.ruleForm).then(res => {
        if (res.data.code === 200) {
          this.$store.commit('setToken', res.data.jdata.token)
          this.$store.commit('setUserInfo', res.data.jdata.userInfo)
          this.$message.success('登录成功')
          if (res.data.jdata.userInfo.role === 'admin') {
            this.$router.push('/list')
          }
        } else {
          this.$message.error('登录失败')
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
